package ui.swing.dnd;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

import ui.swing.Console;

public class CutAndPaste extends JFrame {

	private JMenuBar mb = new JMenuBar();
	private JMenu edit = new JMenu("Edit");
	private JMenuItem cut = new JMenuItem("Cut");
	private JMenuItem copy = new JMenuItem("Copy");
	private JMenuItem paste = new JMenuItem("Paste");
	private JTextArea text = new JTextArea(20, 20);

	private Clipboard clipbd = getToolkit().getSystemClipboard();

	public CutAndPaste() {
		cut.addActionListener(new CutL());
		copy.addActionListener(new CopyL());
		paste.addActionListener(new PasteL());

		edit.add(cut);
		edit.add(copy);
		edit.add(paste);
		mb.add(edit);
		setJMenuBar(mb);
		getContentPane().add(text);
	}

	class CopyL implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			String selection = text.getSelectedText();
			if (selection == null)
				return;
			StringSelection clipString = new StringSelection(selection);
			clipbd.setContents(clipString, clipString);
		}
	}

	class CutL implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			String selection = text.getSelectedText();
			if (selection == null)
				return;
			StringSelection clipString = new StringSelection(selection);
			clipbd.setContents(clipString, clipString);
			text.replaceRange("", text.getSelectionStart(), text
					.getSelectionEnd());
		}
	}

	class PasteL implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Transferable clipData = clipbd.getContents(CutAndPaste.this);
			String clipString;
			try {
				clipString = (String) clipData
						.getTransferData(DataFlavor.stringFlavor);
				text.replaceRange(clipString, text.getSelectionStart(), text
						.getSelectionEnd());
			} catch (UnsupportedFlavorException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		Console.run(new CutAndPaste(), 300, 200);
	}
}
